package com.kuwei.servlet.login;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONObject;
import com.kuwei.model.User;
import com.kuwei.service.LoginService;
import com.kuwei.servlet.frame.BaseServlet;
import com.kuwei.servlet.frame.ControlServlet;
import com.kuwei.utils.CommonUtils;
import com.kuwei.utils.Constants;
import com.kuwei.utils.MD5Encrypt;
import com.kuwei.utils.ReturnResult;
import com.kuwei.utils.ServiceConstants;
import com.kuwei.utils.Utility;

@Controller
@RequestMapping("LoginServlet")
public class LoginServlet extends BaseServlet{
	
	@Autowired
	public LoginService loginService;
	
	private static final Logger logger = LoggerFactory.getLogger(ControlServlet.class);
	
	@RequestMapping("/login.action")
	public void login(HttpServletRequest request,HttpServletResponse response) throws IOException{	
		ReturnResult result = new ReturnResult();
		Map<String, Object> rtnMap = new HashMap<String, Object>();
		Map<String, Object> param = Utility.Params.parseParams(request);
		Map<String,Object> reqMap = new HashMap<String, Object>();
		if(!CommonUtils.isEmpty(param.get("userName"))&&!CommonUtils.isEmpty(param.get("passWd"))){
			reqMap.put("USER_NAME", param.get("userName"));
			try {
				List<Map<String, Object>> list = sqlSessionDaoSupport.getSqlSession().selectList(ServiceConstants.QUERY_USER,reqMap);
				Map<String, Object> userMap = list.get(0);
				String passWd = userMap.get("PASSWD").toString();
				if(passWd.equals(MD5Encrypt.md5Encode(param.get("passWd").toString()))){
					result.setContent(Constants.SUCCESS, "验证通过");
					User user = new User();
					user.setUser_id(userMap.get("USER_ID").toString());
					user.setUser_name(userMap.get("USER_NAME").toString());
					user.setPassWd(userMap.get("PASSWD").toString());
					user.setType(userMap.get("TYPE").toString());
					user.setCreate_date(userMap.get("CREATE_DATE").toString());
					user.setMeal_sub(handleNullString(userMap.get("MEAL_SUB")));
					user.setOver_time_sub(handleNullString(userMap.get("OVER_TIME_SUB")));
					user.setMobile_no(handleNullString(userMap.get("MOBILE_NO")));
					user.setId_card_no(handleNullString(userMap.get("ID_CARD_NO")));
					user.setSex(handleNullString(userMap.get("SEX")));
					user.setReal_name(handleNullString(userMap.get("REAL_NAME")));
					
					request.getSession().setAttribute(Constants.LOGIN_KEY, user);
					logger.debug("用户{}【{}】已登录",user.getUser_name(),user.getUser_id());
				}
			} catch (Exception e){
				result.setContent(Constants.FAIL, e.getMessage());
			}
		} 
		
		rtnMap.put("data", result.getContent());
		PrintWriter out = response.getWriter();
		out.println(JSONObject.toJSONString(rtnMap));
		out.flush();
		out.close();
	}
	@RequestMapping("/loginOut.action")
	public void loginOut(HttpServletRequest request,HttpServletResponse response) throws IOException{	
		Map<String, Object> rtnMap = new HashMap<String, Object>();
		ReturnResult result = new ReturnResult();
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute(Constants.LOGIN_KEY);
		logger.debug("用户{}【{}】已退出登录",user.getUser_name(),user.getUser_id());
		session.removeAttribute(Constants.LOGIN_KEY);
		user = null;
		result.setContent(Constants.SUCCESS, "退出登录");
		rtnMap.put("data", result.getContent());
		rtnMap.put("data", result.getContent());
		PrintWriter out = response.getWriter();
		out.println(JSONObject.toJSONString(rtnMap));
		out.flush();
		out.close();
	}
}
